package com.suprema.biomini;

import java.awt.BasicStroke;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;

/**
 * 指紋畫布，用以顯示掃瞄機之前次掃入之指紋，並可加入黑色邊框
 * @author Kent Yeh
 */
public class FingerCanvas extends Canvas implements Fingerprintable {

    private SFRDevice device;
    private boolean core = false;
    private int padding = 3;
    private int diffX = 0;
    private int diffY = 0;
    private boolean showBorder = true;

    public FingerCanvas(GraphicsConfiguration config, SFRDevice device) {
        super(config);
        this.device = device;
        setPreferredSize(new Dimension(280, 320));
    }

    public FingerCanvas(SFRDevice device) {
        this.device = device;
        setPreferredSize(new Dimension(280, 320));
    }

    @Override
    public boolean isCore() {
        return core;
    }

    /**
     *  Specifies whether the core of fingerprint is drawn of not.   
     * @param core 
     */
    public void setCore(boolean core) {
        this.core = core;
    }

    @Override
    public int getPadding() {
        return padding;
    }

    /**
     * 設定繪圖與邊界的空隙
     * @param padding 空隙
     */
    public void setPadding(int padding) {
        this.padding = padding;
    }

    @Override
    public int getDiffX() {
        return diffX;
    }

    @Override
    public void setDiffX(int diffX) {
        this.diffX = diffX;
    }

    @Override
    public int getDiffY() {
        return diffY;
    }

    @Override
    public void setDiffY(int diffY) {
        this.diffY = diffY;
    }

    @Override
    public int getClientFpLeftTopX() {
        return padding > 0 ? padding : 0;
    }

    @Override
    public int getClientFpLeftTopY() {
        return padding > 0 ? padding : 0;
    }

    @Override
    public int getClientFpBottomRightX() {
        int x = getWidth() - 1 * padding;
        return x > getWidth() ? getWidth() : x;
    }

    @Override
    public int getClientFpBottomRightY() {
        int y = getHeight() - 1 * padding;
        return y > getHeight() ? getHeight() : y;
    }

    /**
     * 繪圖時是否加邊框
     * @return 
     */
    public boolean isShowBorder() {
        return showBorder;
    }

    /**
     * 設定繪圖時是否加邊框
     * @param showBorder 
     */
    public void setShowBorder(boolean showBorder) {
        this.showBorder = showBorder;
    }

    @Override
    public void paint(Graphics g) {
        if (isVisible() && isEnabled() && device!=null && device.isSensorOn()) {
            g.clearRect(0, 0, getWidth(), getHeight());
            int lineWidth = 1;
            if (device != null) {
                g.clearRect(0, 0, getWidth(), getHeight());
                device.draw(this, getClientFpLeftTopX() - diffX + lineWidth, getClientFpLeftTopY() - diffY + lineWidth,
                        getClientFpBottomRightX() - diffX - lineWidth, getClientFpBottomRightY() - diffY - lineWidth, core);
            }
            if (showBorder) {
                Graphics2D g2D = (Graphics2D) g;
                g2D.setStroke(new BasicStroke(lineWidth));
                g2D.drawRect(1, 1, getWidth() - 2, getHeight() - 2);
            }
        }
    }
}
